package com.cat.greedyAlgorithm;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/maximize-the-total-height-of-unique-towers/
 * @create 2025/8/21 16:34
 * @since JDK17
 */

public class Solution08 {
    public long maximumTotalSum(int[] arr) {
        Arrays.sort(arr);
        long ans = 0;
        int n = arr.length, h = arr[n - 1];
        for (int i = n - 1; i >= 0; i--) {
            if (i + 1 > arr[i]) {
                return -1;
            }
            h = Math.min(arr[i], h);
            ans += h--;
        }
        return ans;
    }
}
